home *** CD-ROM | disk | FTP | other *** search
- Path: anvil.ugrad.cs.ubc.ca!not-for-mail
- From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
- Newsgroups: comp.lang.c,comp.unix.programmer
- Subject: Re: Q: '\n' character
- Date: 11 Apr 1996 10:40:00 -0700
- Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
- Message-ID: <4kjg5gINNe3a@anvil.ugrad.cs.ubc.ca>
- References: <31616F63.481D@lava.weeg.uiowa.edu> <4jtddt$eu7@masala.cc.uh.edu> <DpBuF6.83C@ukpsshp1.serigate.philips.nl> <3169994D.665ACF69@cs.ucl.ac.uk>
- NNTP-Posting-Host: anvil.ugrad.cs.ubc.ca
-
- In article <3169994D.665ACF69@cs.ucl.ac.uk>,
- Bill Zissimopoulos <B.Zissimopoulos@cs.ucl.ac.uk> wrote:
- >Stephen Baynes wrote:
- >>
- >> Spasmo (cosc19z5@Bayou.UH.EDU) wrote:
- >> : Artur Wojdat (awojdat@lava.weeg.uiowa.edu) wrote:
- >> : : Hello everybody,
- >> : : Is there a function or some sort of way that I could remove '\n'
- >> : : charecter form the end of the string. I'm reading from two files, want to
- >> : Dunno if there are any functions available, but what I always do
- >> : is just overwrite the '\n' with a '\0', which does the job nicely.
- >>
- >> : For example, let's say that the string that holds the data is called
- >> : buf. To get rid of the '\n' you'd merely do the following:
- >>
- >> : buf[strlen(buf) - 1] = '\0';
- >>
- >> : And that takes care of that.
- >>
- >> Thats the best way, but do check that the character you are about to overwrite
- >> is a newline. There are two occasions when fgets stops without reading a
- >> newline, first if it has filled your buffer (because you have a very long line
- >> in the file), second if there is no newline at the end of the last line of the
- >> file.
- >
- >Which makes this the worst way for the exact reasons that you give.
-
- In fact, if one can still trace back to the original article that started this
- thread, any solutions to the problem involving buffers, string manipulationa nd
- other buffoonery are completely wacky.
-
- The original question, part of which is reveled in the quote above, calls for
- a program that will read lines from two files and display them side by side on
- the standard output---clearly a trivial problem.
-
- This problem can be solved by scanning lines character by character alternating
- between the the left and the right file, and putting them onto standard output
- using just the getc() and putchar() macros. You read until a newline or EOF
- from one file and print, but don't print the newline. Then you do the same
- thing from the second file, and output a newline. If neither file is at EOF,
- you repeat the process. If one file is at EOF but the other is not, you then
- dump the rest of the remaining file to standard output.
-
- Solutions to this problem involving strlen(), fgets() and other zoological
- beasts show a lack of focus and clarity of thought. I recommend a good dose of
- meditation each day, a sound diet, and mandatory readings from a suitable bible
- (a tome dedicated to some area of computer science or another, of course).
-
- The fact is that the problem requires _zero_ input symbols of _lookahead_ in
- either file, hence buffering is only needed for I/O efficiency, not for
- isolating tokens (in this case lines). The efficiency buffering is already
- done by the standard IO library. Further buffering in the application code (in
- this case) is wasteful of memory and CPU cycles, and results in an
- implementation that is unnecessarily complex and hard to read.
-
- For example, the check whether the result of fgets() has a newline or not is
- completely wasteful, and is needed in order to hack what is a poor design in
- the first place into working properly. I agree with you completely about this
- being a bad approach, if not the worst.
- --
-
-